package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.system.domain.WmsInventoryHistory;
import com.ruoyi.system.domain.WmsTransfer;
import com.ruoyi.system.enums.OperationType;
import com.ruoyi.system.enums.WmsTransferStatus;
import com.ruoyi.system.mapper.WmsInventoryHistoryMapper;
import com.ruoyi.system.mapper.WmsTransferMapper;
import com.ruoyi.system.service.IWmsOutboundService;
import com.ruoyi.system.service.IWmsTransferService;
import com.ruoyi.system.utils.GetOrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 移库信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-28
 */
@Service
public class WmsTransferServiceImpl implements IWmsTransferService
{
    @Autowired
    private WmsTransferMapper wmsTransferMapper;

    @Autowired
    private GetOrderNoUtil getOrderNoUtil;

    @Autowired
    private IWmsOutboundService iWmsOutboundService;
    @Autowired
    private WmsInventoryHistoryMapper wmsInventoryHistoryMapper;

    /**
     * 查询移库信息
     *
     * @param id 移库信息主键
     * @return 移库信息
     */
    @Override
    public WmsTransfer selectWmsTransferById(Long id)
    {
        WmsTransfer wmsTransfer = wmsTransferMapper.selectWmsTransferById(id);

        WmsInventoryHistory wmsInventoryHistory = new WmsInventoryHistory();

        wmsInventoryHistory.setOperationId(id);

        List<WmsInventoryHistory> wmsInventoryHistories = wmsInventoryHistoryMapper.selectWmsInventoryHistoryDetailListFromOutbound(wmsInventoryHistory);

        wmsTransfer.setInventoryHistoryList(wmsInventoryHistories);

        return wmsTransfer;

    }

    /**
     * 查询移库信息列表
     *
     * @param wmsTransfer 移库信息
     * @return 移库信息
     */
    @Override
    public TableDataInfo<WmsTransfer> selectWmsTransferList(WmsTransfer wmsTransfer, PageQuery pageQuery)
    {
        Page<WmsTransfer> page = wmsTransferMapper.selectWmsTransferList(pageQuery.build(),wmsTransfer);
        return TableDataInfo.build(page);
    }

    /**
     * 新增移库信息
     * 移库的逻辑无非是 1.先从入库的数据进行出库，2.再将出库的数据挪到另一个仓库的入库中
     *
     * @param wmsTransfer 移库信息
     * @return 结果
     */
    @Override
    public int insertWmsTransfer(WmsTransfer wmsTransfer)
    {

        //新增移库信息
        if (WmsTransferStatus.COMPLETED.getCode().equals(wmsTransfer.getTransferStatus())) {
            wmsTransfer.setCreateTime(new Date());
            wmsTransfer.setTransferUserId(LoginHelper.getUserId());
        }

        int i = wmsTransferMapper.insertWmsTransfer(wmsTransfer);
        //插入二维码(待定)

        //移库：先从入库数据中出库
        outboundData(wmsTransfer);



        return i;
    }

    /**
     * 移库：先从入库数据中出库
     */
    public void outboundData(WmsTransfer wmsTransfer){

        for (WmsInventoryHistory wmsInventoryHistory : wmsTransfer.getInventoryHistoryList()){

            wmsInventoryHistory.setInboundId(wmsInventoryHistory.getId());
            //修改库存记录的出库数量字段
            iWmsOutboundService.editOutboundTotalById(wmsInventoryHistory);

            wmsInventoryHistory.setOperationId(wmsTransfer.getId());
            wmsInventoryHistory.setOperationNo(wmsTransfer.getTransferOrderNo());
            wmsInventoryHistory.setOperationType(OperationType.TRANSFER.getCode());
            wmsInventoryHistory.setCreateTime(new Date());
            wmsInventoryHistory.setId(null);
            wmsInventoryHistoryMapper.insertWmsInventoryHistory(wmsInventoryHistory);
        }
    }


    /**
     * 修改移库信息
     *
     * @param wmsTransfer 移库信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateWmsTransfer(WmsTransfer wmsTransfer)
    {
        //新增移库信息
        if(WmsTransferStatus.COMPLETED.getCode().equals(wmsTransfer.getTransferStatus())){
            wmsTransfer.setCreateTime(new Date());
            wmsTransfer.setTransferUserId(LoginHelper.getUserId());
        }

        WmsInventoryHistory wmsInventoryHistory = new WmsInventoryHistory();
        wmsInventoryHistory.setOperationId(wmsTransfer.getId());

        List<WmsInventoryHistory> wmsInventoryHistorieList = wmsInventoryHistoryMapper.selectWmsInventoryHistoryDetailListFromOutbound(wmsInventoryHistory);

        //先将数量还原回去
        for (WmsInventoryHistory inventoryHistory : wmsInventoryHistorieList) {
            WmsInventoryHistory editInventoryHistory = new WmsInventoryHistory();
            editInventoryHistory.setId(inventoryHistory.getInboundId());
            editInventoryHistory.setOutboundTotal(inventoryHistory.getTotalQuantity());
            wmsInventoryHistoryMapper.editSubtractOutboundTotal(editInventoryHistory);
        }

        Long[] ids = wmsInventoryHistorieList.stream().map(WmsInventoryHistory::getId)  // 提取每个对象的id
                .toArray(Long[]::new);

        //删除之前的商品明细
        wmsInventoryHistoryMapper.deleteWmsInventoryHistoryByIds(ids);

        //重新插入移库数据
        for (WmsInventoryHistory wmsInventoryHistoryDetail : wmsTransfer.getInventoryHistoryList()) {
            //修改库存记录的出库数量字段
            iWmsOutboundService.editOutboundTotalById(wmsInventoryHistoryDetail);

            wmsInventoryHistoryDetail.setOperationId(wmsTransfer.getId());
            wmsInventoryHistoryDetail.setOperationNo(wmsTransfer.getTransferOrderNo());
            wmsInventoryHistoryDetail.setOperationType(OperationType.TRANSFER.getCode());
            wmsInventoryHistoryDetail.setCreateTime(new Date());
            wmsInventoryHistoryDetail.setId(null);
            wmsInventoryHistoryMapper.insertWmsInventoryHistory(wmsInventoryHistoryDetail);
        }

        return wmsTransferMapper.updateWmsTransfer(wmsTransfer);
    }

    /**
     * 批量删除移库信息
     *
     * @param ids 需要删除的移库信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsTransferByIds(Long[] ids)
    {
        return wmsTransferMapper.deleteWmsTransferByIds(ids);
    }

    /**
     * 删除移库信息信息
     *
     * @param id 移库信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsTransferById(Long id)
    {
        return wmsTransferMapper.deleteWmsTransferById(id);
    }


    /**
     * 获取移库单编号
     * @return
     */
    @Override
    public WmsTransfer getTransferOrderNo() {
        WmsTransfer wmsTransfer = new WmsTransfer();
        wmsTransfer.setTransferOrderNo(getOrderNoUtil.getOrderNo("YK-"));
        return wmsTransfer;
    }


    /**
     * 作废
     */
    @Override
    @Transactional
    public int cancellation(Long id){
        WmsTransfer wmsTransfer = new WmsTransfer();
        wmsTransfer.setId(id);
        wmsTransfer.setTransferStatus(WmsTransferStatus.CANCELED.getCode());

        WmsInventoryHistory wmsInventoryHistory = new WmsInventoryHistory();
        wmsInventoryHistory.setOperationId(wmsTransfer.getId());

        List<WmsInventoryHistory> wmsInventoryHistorieList = wmsInventoryHistoryMapper.selectWmsInventoryHistoryDetailListFromOutbound(wmsInventoryHistory);

        //对出库的商品进行累减操作
        for (WmsInventoryHistory inventoryHistory : wmsInventoryHistorieList) {
            WmsInventoryHistory editInventoryHistory = new WmsInventoryHistory();
            editInventoryHistory.setId(inventoryHistory.getInboundId());
            editInventoryHistory.setOutboundTotal(inventoryHistory.getTotalQuantity());
            wmsInventoryHistoryMapper.editSubtractOutboundTotal(editInventoryHistory);
        }
        //修改出库信息的状态，为作废
        wmsTransfer.setCreateTime(new Date());
        wmsTransfer.setTransferUserId(LoginHelper.getUserId());
        return wmsTransferMapper.updateWmsTransfer(wmsTransfer);
    }
}
